package com.edeweiss.community.servlet;

import com.edeweiss.community.domain.Message;
import com.edeweiss.community.domain.Report;
import com.edeweiss.community.domain.Student;
import com.edeweiss.community.services.ReportService;
import com.edeweiss.community.services.servicesImp.ReportServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@WebServlet("/ReportServlet")
public class ReportServlet extends BaseServlet {
    private ReportService reportService = new ReportServiceImpl();

   public String report(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       //定义过滤HTML标签的正则表达式
       String regHtml = "<([^>]*)>";
       //获取举报的参数
       Student student = (Student)request.getSession().getAttribute("student");
       if(student == null){
           //学生没有登陆，直接返回到登陆页面
           return "loginForm.jsp";
       }else {
           //获取举报学生的id
           String sid = student.getSid();
           //获取举报的数据
           String content = request.getParameter("editorValue");
           content = content.replaceAll(regHtml, "");
           //封装对象，进行查询
           Report report = new Report();
           report.setSid(sid);
           report.setContent(content);
           report.setTime(new Date());
           //调用service进行举报
           int flag = reportService.report(report);
           if(flag == 1){
               request.setAttribute("msg","举报成功，请等候处理");
           }else {
               request.setAttribute("msg","举报失败，请重新尝试");
           }
           return "/Report.jsp";
       }
   }
    public String answer(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取反馈的内容
        String feedback = request.getParameter("feedback");
        //2.获取回复消息的id
        String rid = request.getParameter("rid");
        //获取回复的学生id
        String sid = request.getParameter("sid");
        Message message = new Message();

        message.setMessage(feedback);
        message.setSid(sid);
        message.setTime(new Date());
        message.setStatus(0);

        //调用service层的反馈方法
        int flag = reportService.answer(message,Integer.parseInt(rid));

        String msg=flag==1?"反馈成功,请接着处理下一条消息":"反馈失败";

        request.getSession().setAttribute("msg",msg);
        response.sendRedirect("pages/index.jsp");
        return "";
    }
    public String findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.调用service查询所有的举报
        List<Report> reports = reportService.findAll();
        if(reports == null || reports.size() <= 0){
            request.getSession().setAttribute("msg","暂时没有要举报的消息！");
            response.sendRedirect("pages/index.jsp");

        }else {
            request.getSession().setAttribute("reports",reports);
            response.sendRedirect("pages/reportHandle.jsp");
        }
        return "";
    }
}
